package com.orangelife.mobile.parkingcard.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.curry.android.adapter.FatherAdapter;
import com.curry.android.util.IntentHelper;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.parkingcard.activity.ParkingCardDetailActivity;
import com.orangelife.mobile.parkingcard.activity.ParkingCardFeeActivity;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HaveCardListAdapter<T> extends FatherAdapter<T> {
	
	/**list的数据*/
	private List<Map<String,Object>> list = null;
	
	private Context context;
	
	private OnLongClickListenerInterface onLongClickListenerInterface;
	
	public HaveCardListAdapter(Context context,List<Map<String,Object>> list,
			OnLongClickListenerInterface onLongClickListenerInterface){
		
		this.context = context;
		this.list = list;
		this.onLongClickListenerInterface = onLongClickListenerInterface;
	}
	
	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressWarnings("unchecked")
	@Override
	public BaseAdapter setList(List<T> list) {
		this.list = (List<Map<String, Object>>) list;
		return HaveCardListAdapter.this;
	}
	
	@SuppressWarnings("unchecked")
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.item_lv_add_parking_card, null);
			holder.tvTime = (TextView) convertView.findViewById(R.id.tvTime);
			holder.tvPayment = (TextView) convertView.findViewById(R.id.tvPayment);
			holder.tvCarNumber = (TextView) convertView.findViewById(R.id.tvCarNumber);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		final Map<String,Object> map = list.get(position);
		String endDay = String.valueOf(map.get("expiration_time"));
		int status=0;
		if(map.get("status")!=null&& !"".equals(map.get("status")+"")) {
			status= Integer.parseInt(map.get("status")+"");
		}
		int expration = Integer.parseInt(map.get("expiration")+"");
		
		
		if(status == 1 && expration == 0){
			holder.tvTime.setText("有效期至"+endDay);
			holder.tvTime.setTextColor(context.getResources().getColor(R.color.DarkGray));
			holder.tvCarNumber.setBackgroundColor(context.getResources().getColor(R.color.BtnOrange));
		}else{
			holder.tvTime.setText("待缴费");
			holder.tvTime.setTextColor(context.getResources().getColor(R.color.lightGray));
			holder.tvCarNumber.setBackgroundColor(context.getResources().getColor(R.color.lightGray));
		}
		
		holder.tvCarNumber.setText(map.get("num_plate").toString());
		holder.tvCarNumber.setOnClickListener(new Listener(map,holder));
		holder.tvPayment.setOnClickListener(new Listener(map,holder));
		holder.tvCarNumber.setOnLongClickListener(new LongListener(position, map));
		return convertView;
	}
	
	public class LongListener implements OnLongClickListener{

		private int position;
		private Map<String,Object> map;
		
		public LongListener(int position,Map<String,Object> map){
			this.position = position;
			this.map = map;
		}
		
		@Override
		public boolean onLongClick(View view) {
			switch (view.getId()) {
			case R.id.tvCarNumber:
				onLongClickListenerInterface.delete(position, map);
				break;

			default:
				break;
			}
			return false;
		}
		
	}
	
	public class Listener implements OnClickListener{
		
		Map<String,Object> maps = new HashMap<String, Object>();
		ViewHolder holder;
		
		public Listener(Map<String,Object> maps,ViewHolder holder){
			this.maps = maps;
			this.holder = holder;
		}

		@Override
		public void onClick(View view) {
			
			switch (view.getId()) {
			case R.id.tvPayment:
				//进入充值缴费
				Bundle bundle = new Bundle();
				Map<String,Object> map = new HashMap<String, Object>();
				map.put("carNumber",maps.get("num_plate").toString());
				map.put("parkingType",maps.get("parkingName").toString());
				map.put("parkID", maps.get("pl_ter_id").toString());
				map.put("cardsID",maps.get("id").toString());
				IntentHelper.getInstances().bundleWithExtra(context, ParkingCardFeeActivity.class, bundle, (Serializable) map);
				break;
				
			case R.id.tvCarNumber:
				//进入停车卡详情
				Bundle bundle1 = new Bundle();
				Map<String,Object> map1 = new HashMap<String, Object>();
				map1.put("cardsID",maps.get("id").toString());
				map1.put("parkID", maps.get("pl_ter_id").toString());
				map1.put("number",maps.get("number").toString());
				map1.put("carNumber",maps.get("num_plate").toString());
				map1.put("parkingType",maps.get("parkingName").toString());
				map1.put("expiration_time",holder.tvTime.getText().toString());
				IntentHelper.getInstances().bundleWithExtra(context,ParkingCardDetailActivity.class, bundle1, (Serializable) map1);
				break;

			default:
				break;
			}
		}
		
	}

	class ViewHolder {
		/** 停车卡缴费时间 */
		private TextView tvTime;
		/** 充值缴费*/
		private TextView tvPayment;
		/** 车牌号*/
		private TextView tvCarNumber;
	}
	
	public interface OnLongClickListenerInterface{
		public void delete(int position, Map<String, Object> map);
	}

}
